// @ts-nocheck
import React, { useState } from 'react';
import { TextArea, Toast } from 'antd-mobile';
import { Uploader } from 'react-vant';
import server from '@/utlis/server';
import jwtDecode from 'jwt-decode';
import './index.css';
import './index.less';
import '@/pages/golobo.less';
import 'lib-flexible';
import { history } from 'umi';
import '@/pages/golobo.less';
import moment from 'moment';

export default () => {
  const [title, settitle] = useState('');
  const [defaultValue, setdefaultValue] = useState([]);
  let upload = async (file: File) => {
    // console.log(file);
    try {
      let obj = {
        url: '',
      };
      const body = new FormData();
      body.append('file', file);
      let { data } = await server.post('/img', body);
      // console.log(data, 'image');
      obj.url = 'http://localhost:3000/' + data.imgurl;
      return obj;
    } catch (error) {
      return { url: `demo_path/${file.name}` };
    }
  };
  // 添加评论
  let addmoveon = async () => {
    let token = localStorage.getItem('token');
    if (token) {
      let userlo = jwtDecode(token)._doc;
      let user = {
        _id: userlo._id,
        username: userlo.username,
        img: userlo.img,
      };
      let form = {
        user,
        title: title,
        imgs: defaultValue.map((item) => {
          return item.url;
        }),
        time: moment().format('YYYY-MM-DD'),
      };
      // console.log(form,'form12ee');
      let { data } = await server.post('/addmoveon', form);
      Toast.show({
        icon: 'success',
        content: '添加成功',
      });
      history.push('/discover');
    }
  };
  return (
    <div>
      <div style={{ textAlign: 'center', fontSize: '0.5rem' }}>
        <span>标题</span>
      </div>
      <div className="header">
        <TextArea
          value={title}
          placeholder="请输入标题"
          autoSize={{ minRows: 3, maxRows: 5 }}
          onChange={(v) => {
            settitle(v);
          }}
          showCount={true}
        />
      </div>
      <div
        style={{ textAlign: 'center', fontSize: '0.5rem', marginTop: '0.3rem' }}
      >
        <span>文章内容</span>
      </div>
      <div className="context">
        <Uploader
          accept="*"
          maxCount={6}
          value={defaultValue}
          multiple={true}
          onChange={(v) => setdefaultValue([...v])}
          upload={upload}
        />
      </div>
      <div className="btm">
        <button
          onClick={() => {
            history.push('/discover');
          }}
        >
          取消
        </button>
        <button
          onClick={() => {
            addmoveon();
          }}
        >
          确定
        </button>
      </div>
    </div>
  );
};
